package asteroids.model.programs.parsing.expressions;

import java.util.List;
import java.util.Map;

import be.kuleuven.cs.som.annotate.Basic;

import asteroids.model.programs.parsing.types.Type;

/**
 * A class of binary expressions.
 * A binary expression involves a single operator applied to a left-hand expression and a right-hand expression.
 * 
 * @author Thijs Dieltjens - Wim Konings
 * @version 2.0
 */
public abstract class BinOpExpr extends Expression {

	public BinOpExpr(Expression e1, Expression e2,int line, int column) {
		super(line,column);
		this.leftOperand = e1;
		this.rightOperand= e2;		
	}

	@Basic
	public Expression getLeftOperand(){
		return leftOperand;
	}

	private Expression leftOperand;

	@Basic
	public Expression getRightOperand(){
		return rightOperand;
	}

	private Expression rightOperand;

	@Override
	public  List<String> getTypeCheckErrors(List<String> errors, Map<String, Type> globals) {
		errors = getLeftOperand().getTypeCheckErrors(errors, globals);
		errors = getRightOperand().getTypeCheckErrors(errors, globals);
		try{
			if(getLeftOperand().eval(null, globals).getClass() != getRightOperand().eval(null, globals).getClass()){
				errors.add(getLine()+"-"+getColumn()+": Two operands are not of same type.");
			}
		}
		catch(NullPointerException e){
			errors.add("No valid given operands for this binary operation.");
		}
		return errors;
	}
}
